package com.beyond.trace.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.domain.TracePlantRecord;
import com.beyond.trace.service.ITracePlantRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 农事记录信息Controller
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/trace/record")
@Api(value = "农事记录信息", tags = "农事记录信息")
public class TracePlantRecordController extends BaseController
{
    @Autowired
    private ITracePlantRecordService tracePlantRecordService;

    //@PreAuthorize("@ss.hasPermi('beyond:record:list')")
    @GetMapping("/list")
    @ApiOperation("查询农事记录信息列表")
    public TableDataInfo list(TracePlantRecord tracePlantRecord)
    {
        startPage();
        List<TracePlantRecord> list = tracePlantRecordService.selectTracePlantRecordList(tracePlantRecord);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('beyond:record:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询农事记录信息列表不分页")
    public AjaxResult getInfoQuery(TracePlantRecord tracePlantRecord)
    {
        return AjaxResult.success(tracePlantRecordService.selectTracePlantRecordList(tracePlantRecord));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:record:export')")
    @Log(title = "农事记录信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TracePlantRecord tracePlantRecord)
    {
        List<TracePlantRecord> list = tracePlantRecordService.selectTracePlantRecordList(tracePlantRecord);
        ExcelUtil<TracePlantRecord> util = new ExcelUtil<TracePlantRecord>(TracePlantRecord.class);
        return util.exportExcel(list, "农事记录信息数据");
    }

    //@PreAuthorize("@ss.hasPermi('beyond:record:query')")
    @GetMapping(value = "/{recordId}")
    @ApiOperation("查询农事记录信息列表")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(tracePlantRecordService.selectTracePlantRecordById(recordId));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:record:add')")
    @Log(title = "农事记录信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增农事记录信息")
    public AjaxResult add(@RequestBody @Validated TracePlantRecord tracePlantRecord)
    {
        if(StringUtils.isBlank(tracePlantRecord.getRecordProduct())){
            return AjaxResult.error("请选择产品");
        }else{
            return toAjax(tracePlantRecordService.insertTracePlantRecord(tracePlantRecord));
        }
    }

    //@PreAuthorize("@ss.hasPermi('beyond:record:edit')")
    @Log(title = "农事记录信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改农事记录信息")
    public AjaxResult edit(@RequestBody TracePlantRecord tracePlantRecord)
    {
        return toAjax(tracePlantRecordService.updateTracePlantRecord(tracePlantRecord));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:record:remove')")
    @Log(title = "农事记录信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    @ApiOperation("删除农事记录信息")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(tracePlantRecordService.deleteTracePlantRecordByIds(recordIds));
    }

    @Log(title = "种植记录图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("种植记录图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/plantRecord", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
